package com.anlogic.sdk.ui.tests.uc1.workspacemgr;

import static org.junit.Assert.*;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.anlogic.sdk.env.utils.EnvCheckUtils;
import com.anlogic.sdk.ui.tests.utils.WorkspaceUtils;
import com.anlogic.sdk.utils.EnvUtils;
import com.anlogic.sdk.utils.UIUtils;
import com.anlogic.sdk.workspace.FDWorkspace;

public class UC1_TC002_Test {
	
	private static SWTWorkbenchBot bot;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {

		bot = new SWTWorkbenchBot();
		SWTBotPreferences.TIMEOUT = 10000;
		SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"), 90 * 1000);
	//	SWTBotPreferences.PLAYBACK_DELAY = 10;
		
	}

	@Before
	public void setUp() throws Exception {
		WorkspaceUtils.resetworkbench(bot);
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		
	}


	@After
	public void tearDown() throws Exception {
		
		WorkspaceUtils.cleanWorkspace();
	}

	@Test
	public void checkWorkspaceTest() throws Exception{
		try {
			checkWorkspace();
		} catch (Exception e) {
			assertTrue(false);
		}
		
		assertTrue(true);
	}
	
	public void checkWorkspace() {
		
		EnvCheckUtils envCheckUtils = new EnvCheckUtils();
		String msg = envCheckUtils.checkWorkspace();
		IProject[] projects = EnvUtils.getWorkspaceProjects();
		IProject[] unMatchedProjects = EnvUtils.getUnMatchedProjects();
		if (!msg.isEmpty() && projects.length != 0) {
			msg = "The workspace version is inconsistent with the current software version. Projects may not open or compile properly. Click OK to reset the workspace and projects, click Cancel to open only the workspace.";
			openResetDialog(msg);
		} else if (projects.length != 0 && projects.length == unMatchedProjects.length) {
			msg = "The workspace may be opened for the first time. Click OK to reset the workspace and projects, click Cancel to open only the workspace.";
			openResetDialog(msg);
		} else if (projects.length == 0 && envCheckUtils.wsHasUnimportedProjects()) {
			msg = "The workspace may be opened for the first time. Click OK to import projects and reset the workspace, click Cancel to open only the workspace.";
			openImportResetDialog(msg);
		}
		
		FDWorkspace ws = new FDWorkspace();
		if (!ws.propertyFileExists()) {
			ws.createNewPropertyFile();
		}
		
		envCheckUtils.reportProjectProblem(EnvUtils.getWorkspaceProjects());
		
	}
	public void openResetDialog(String msg) {
		EnvCheckUtils envCheckUtils = new EnvCheckUtils();
		boolean ret = MessageDialog.openConfirm(null, "Workspace Check", msg);
		if (ret) {
			Job job = new Job("Reset Workspace") {
				@Override
				protected IStatus run(IProgressMonitor monitor) {
					
					String errMsg = envCheckUtils.resetWorkspace();
					if (errMsg != null && !errMsg.isEmpty()) {
						UIUtils.showMessageDialogAsync("Reset workspace", errMsg, MessageDialog.ERROR);
					} else {
						UIUtils.showMessageDialogAsync("Reset workspace", "Reset workspace successfully!", MessageDialog.NONE);
					}
					return Status.OK_STATUS;
				}	
			};
			job.setUser(true);
			job.schedule();
		}
	}
	
	public void openImportResetDialog(String msg) {
		EnvCheckUtils envCheckUtils = new EnvCheckUtils();
		boolean ret = MessageDialog.openConfirm(null, "Workspace Check", msg);
		if (ret) {
			Job job = new Job("Reset Workspace") {
				@Override
				protected IStatus run(IProgressMonitor monitor) {
					String errMsg = envCheckUtils.importProjects(EnvUtils.getWorkspacePath());
					if (errMsg != null && !errMsg.isEmpty()) {
					}
					errMsg = envCheckUtils.resetWorkspace();
					if (errMsg != null && !errMsg.isEmpty()) {
						UIUtils.showMessageDialogAsync("Reset workspace", errMsg, MessageDialog.ERROR);
					} else {
						UIUtils.showMessageDialogAsync("Reset workspace", "Reset workspace successfully!", MessageDialog.NONE);
					}
					return Status.OK_STATUS;
				}	
			};
			job.setUser(true);
			job.schedule();
		}
	}

}
